package com.ditian.code;
/**
 * 错误信息
 * @author chenweiyi
 *
 */
public enum ErrorMessage {
	
	BAD_ARGUMENTS(400,"BAD_ARGUMENTS:<key>","某个参数解析出错（比如必须是数字，但是输入的是非数字字符串; 或者长度过长，etc."),
	
	COEXISTENCE_ARGUMENTS(400,"COEXISTENCE_ARGUMENTS","同时传入了要求是二选一或多选一的参数。如有特殊说明则不返回此错误。"),
	
	EMPTY_FACESET(400,"EMPTY_FACESET","Faceset中没有face_token"),
	
	FACESET_EXIST(400,"FACESET_EXIST","想要创建的FaceSet已经存在（当提供了一个当前存储空间内下已存在的outer_id，且force_merge为0时）"),
	
	FACESET_NOT_EMPTY(400,"FACESET_NOT_EMPTY","FaceSet不为空，不能删除（设置check_empty=1时）"),
	
	FACESET_QUOTA_EXCEEDED(400,"FACESET_QUOTA_EXCEEDED","FaceSet数量达到上限，不能继续创建FaceSet"),
	
	IMAGE_ERROR_UNSUPPORTED_FORMAT(400,"IMAGE_ERROR_UNSUPPORTED_FORMAT:<param>","参数<param>对应的图像无法正确解析，有可能不是一个图像文件、或有数据破损。"),
	
	IMAGE_FILE_TOO_LARGE(400,"IMAGE_FILE_TOO_LARGE","客户上传的图片文件太大。本 API 要求图片文件大小不超过 2 MB"),
	
	INVALID_FACESET_TOKEN(400,"INVALID_FACESET_TOKEN","使用faceset_token作为参数时，所传的faceset_token不存在"),
	
	INVALID_FACE_TOKEN(400,"INVALID_FACE_TOKEN: <face_token>","使用face_token作为参数时，所传的face_token不存在。"),
	
	INVALID_FACE_TOKENS_SIZE(400,"INVALID_FACE_TOKENS_SIZE","face_tokens数组长度不符合要求"),
	
	INVALID_IMAGE_SIZE(400,"INVALID_IMAGE_SIZE:<param>","客户上传的图像像素尺寸太大或太小，图片要求请参照本API描述。<param>对应图像太大的那个参数的名称"),
	
	INVALID_IMAGE_URL(400,"INVALID_IMAGE_URL","无法从指定的image_url下载图片，图片URL错误或者无效"),
	
	INVALID_OUTER_ID(400,"INVALID_OUTER_ID","使用outer_id作为参数时，所传的outer_id不存在"),
	
	MISSING_ARGUMENTS(400,"MISSING_ARGUMENTS: <key>","缺少某个必选参数。"),
	
	NEW_OUTER_ID_EXIST(400,"NEW_OUTER_ID_EXIST","提供的new_outer_id与已有outer_id重复"),
	
	REQUEST_DECRYPT_FAILD(400,"REQUEST_DECRYPT_FAILD","请求数据解码失败"),
	
	VOID_REQUEST(400,"VOID_REQUEST","VOID_REQUEST	传入return_landmark=0且return_attributes=none导致不进行任何人脸分析操作"),
	
	WRONG_REQUEST_DATA(400,"WRONG_REQUEST_DATA","请求数据有误"),
	
	WRONG_SDK_TYPE(400,"WRONG_SDK_TYPE","传入的 sdk_type 错误，必须是合法值，且与正在使用的 SDK 产品相一致（注意不要使用旧版 SDK V1）"),
	
	AUTHENTICATION_ERROR(401,"AUTHENTICATION_ERROR","api_key和api_secret不匹配"),
	
	AUTHORIZATION_ERROR(403,"AUTHORIZATION_ERROR:<reason>","api_key没有调用本API的权限，具体原因为：用户自己禁止该api_key调用、管理员禁止该api_key调用、由于账户余额不足禁止调用。"),
	
	BUNDLE_NOT_EXIST(403,"BUNDLE_NOT_EXIST","bundleid不存在\n注：请先在Face++ 控制台 “应用管理”菜单下“Bundle ID”中添加应用的Bundle ID。"),
	
	CONCURRENCY_LIMIT_EXCEEDED(403,"CONCURRENCY_LIMIT_EXCEEDED","并发数超过限制。"),
	
	DAILY_QUOTA_EXCEEDED(403,"DAILY_QUOTA_EXCEEDED","该免费 API Key 每天能够进行免费授权的限额已被耗尽，当天无法再进行免费授权。"),
	
	INVALID_DURATION(403,"INVALID_DURATION","传入的 auth_duration 无效。"),
	
	LICENSE_NOT_FOUND(403,"LICENSE_NOT_FOUND","使用正式 API Key 调用时，该账户没有与传入的 sdk_type 和 auth_duration 相应的次数包。"),
	
	API_NOT_FOUND(404,"API_NOT_FOUND","所调用的API不存在。"),
	
	IMAGE_DOWNLOAD_TIMEOUT(412,"IMAGE_DOWNLOAD_TIMEOUT","下载图片超时"),
	
	REQUEST_ENTITY_TOO_LARGE(413,"Request Entity Too Large","客户发送的请求大小超过了2MB限制。该错误的返回格式为纯文本，不是json格式。"),
	
	INTERNAL_ERROR(500,"INTERNAL_ERROR","服务器内部错误，当此类错误发生时请再次请求，如果持续出现此类错误，请及时联系技术支持团队。");
	
	private int status;
	
	private String message;
	
	private String describe;

	private ErrorMessage(int status, String message, String describe) {
		this.status = status;
		this.message = message;
		this.describe = describe;
	}

	private static ErrorMessage [] errorMessages = ErrorMessage.values();
	
	public int getStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}

	public String getDescribe() {
		return describe;
	}
	
	/**
	 * 根据状态或信息获取指定的错误
	 * @param status
	 * @param message
	 * @return
	 */
	public static ErrorMessage messageOf(int status,String message){
		
		if(message==null || "".equals(message)){
			return null;
		}
		
		int len=errorMessages.length,left=0,right=len;
		message = message.replaceAll(":\\s*<?(\\w*\\s*)*>?", "").toUpperCase().replaceAll("\\s+", "_");
		while(len>0&&left<=right){
			
			int mid = (left+right)/2;
			ErrorMessage e = errorMessages[mid];
			int s = e.getStatus();
			String m = e.toString();
			if(status > s){
				left = mid + 1;
				
			}else if(status < s){
				right = mid - 1;
			}else{
				if(message.compareTo(m)>0){
					left = mid + 1;
				}else if(message.compareTo(m)<0){
					right = mid - 1;
				}else{
					return e;
				}
			}
		}
		
		return null;
	}

}
